Validate hotspots() kernel like apply()/focal_stats()#2799
Merged
Conversation
hotspots() never ran its kernel through custom_kernel(), so kernel=None and a list-of-list kernel raised AttributeError on kernel.shape, an even-dimensioned kernel silently succeeded, and a zero-sum kernel divided by zero during normalization. Run custom_kernel(kernel) before the memory check and add a zero-sum guard (hotspots is the only focal function that normalizes by kernel.sum()). Add regression tests for the None, list-of-list, even-dim, and zero-sum cases plus a valid-kernel happy path.
brendancol
commented
Jun 1, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
PR Review: Validate hotspots() kernel like apply()/focal_stats()
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
None.
Nits (optional improvements)
- focal.py:1509 -- the guard catches exactly-zero sums. A mixed-sign kernel that sums to roughly zero but not exactly zero (floating-point near-cancellation) still slips through and produces a near-infinite normalization. This matches focal_stats() and is within the issue scope, so leaving it is fine.
What looks good
- Validation runs before backend dispatch, so numpy, cupy, dask+numpy, and dask+cupy all reject bad kernels the same way. The 3D path recurses through hotspots() per band, so each band validates too.
- custom_kernel() runs before kernel.sum(), so the sum call always has an ndarray. No AttributeError risk.
- Error messages name the function and explain the failure.
- Tests cover None, list-of-list, even-dim, and zero-sum kernels plus a valid happy path. Full test_focal.py passes (155).
Checklist
- Algorithm unchanged; only input validation added
- All backends get consistent validation (runs before dispatch)
- NaN handling unchanged
- Edge cases covered by tests
- Dask path unaffected (validation is pre-dispatch)
- No premature materialization
- Benchmark not needed (validation-only change)
- README feature matrix not applicable (no new function)
- Docstrings present (existing hotspots docstring unchanged)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hotspots() skipped the kernel validation that apply() and focal_stats() run via custom_kernel(), so bad kernels produced confusing errors or wrong behavior:
Backend coverage
The validation runs before backend dispatch, so it applies to numpy, cupy, dask+numpy, and dask+cupy.
Test plan
Closes #2771